/*
Based on code in basic_reduced_form_analysis.do
- Focus on Vytorin and generic Zocor entry events
- Give detailed breakdown for control group
*/

set more off
clear
use user_quarter_choice_cholesterol


// focus on cholesterol
tab indic_id
keep if indic_id == 5



// BASIC: LOOK AT IMPACT OF FIRST CHOICE ON LATER CHOICE 
egen first_q = min(quarter), by(enrolid)
gen first_choice_temp = generic_id if quarter == first_q
egen first_choice = max(first_choice_temp), by(enrolid)

gen first_generic_temp = generic if quarter == first_q
egen first_generic = max(first_generic_temp), by(enrolid)

gen first_xr_temp = xr if quarter == first_q
egen first_xr = max(first_xr_temp), by(enrolid)


gen period = quarter - first_q
gen chose_initial = (generic_id == first_choice) & (generic == first_generic) & (xr == first_xr)

gen initial_generic_temp = generic if quarter == first_q
egen initial_generic_user = max(initial_generic_temp), by(enrolid)

drop first_choice_temp

tab first_choice





/////////////////////////////////////////////
// IDENTIFIED ANALYSIS: USING ENTRY EVENTS //
/////////////////////////////////////////////



// need name for overlap analysis
merge m:1 generic_id using generic_name_mapping
keep if _merge == 3
drop _merge


rename generic_id curr_generic_id
rename gennme curr_gennme
rename generic curr_generic
rename xr curr_xr
drop drugid

// user is in the dataset only based on first use
joinby enrolid indic_id using "user_treatment_indicators_cholesterol"


// filter out irrelevant entries
// 1) not either treatment or control for that entry event
keep if treat == 1 | control == 1
replace treat = 0 if control == 1

//replace treat_alt = 0 if control_alt == 1

// 2) quarter came before entry quarter
gen entry_quarter = qofd(entry_date)
gen rel_quart = quarter - entry_quarter
//keep if rel_quart >= 0

// A) generate the outcomes
gen chose_entry_drug = (curr_generic_id == generic_id) & (curr_generic == generic) & (curr_xr == xr)
tab chose_entry_drug


// B) analyze
gen treat_generic = treat * generic 
gen treat_xr = treat * xr
// is stickiness bigger for generic choice? (doesn't have to be)


// Does this need correcting?
gen initial = chose_entry_drug if quarter == first_q  // rel_quart == 0 (old code; this is probably cleaner; use their first choice, otherwise some not there in period 0)
egen initial_choice = max(initial), by(enrolid entry_num) // unnecessary? maybe only 1 entry_num per enrolid



/*
PRODUCE NEW GRAPHS
*/

// 1) Vytorin breakdown
preserve
keep if entry_num == 10

// first_choice, first_generic
// Lipitor: ; Zocor: ; Crestor: ; Vytorin: 3658

gen group = 1 if treat == 1
replace group = 2 if (treat==0) & (first_choice == 6585)
replace group = 3 if (treat==0) & (first_choice == 959)
replace group = 4 if (treat==0) & (first_choice == 6436)

collapse (mean) chose_entry_drug, by(group rel_quart)


twoway (line chose_entry_drug rel_quart if group == 1 & rel_quart > -5 & rel_quart <= 16) ///
(line chose_entry_drug rel_quart if group == 2 & rel_quart > -5 & rel_quart <= 16) ///
(line chose_entry_drug rel_quart if group == 3 & rel_quart > -5 & rel_quart <= 16) ///
(line chose_entry_drug rel_quart if group == 4 & rel_quart > -5 & rel_quart <= 16), ///
xtitle("Quarter Relative to Drug Entry",size(large)) ytitle("Fraction on Vytorin",size(large)) ///
 xlabel(,labsize(large)) ylabel(,labsize(large)) ///
legend(label(1 "Treated") label(2 "Control Z") label(3 "Control L") label(4 "Control C"))

graph export "event_study_results_vytorin_breakdown.pdf", replace
graph save "event_study_results_vytorin_breakdown", replace

restore


// 2) Generic Zocor breakdown
preserve
keep if entry_num == 12

gen group = 1 if treat == 1
replace group = 2 if (treat==0) & (first_choice == 6585) & first_generic == 0
replace group = 3 if (treat==0) & (first_choice == 959)
replace group = 4 if (treat==0) & (first_choice == 6436)
replace group = 5 if (treat==0) & (first_choice == 3658)

collapse (mean) chose_entry_drug, by(group rel_quart)

twoway (line chose_entry_drug rel_quart if group == 1 & rel_quart > -5 & rel_quart <= 16) ///
(line chose_entry_drug rel_quart if group == 2 & rel_quart > -5 & rel_quart <= 16) ///
(line chose_entry_drug rel_quart if group == 3 & rel_quart > -5 & rel_quart <= 16) ///
(line chose_entry_drug rel_quart if group == 4 & rel_quart > -5 & rel_quart <= 16) ///
(line chose_entry_drug rel_quart if group == 5 & rel_quart > -5 & rel_quart <= 16), ///
	xtitle("Quarter Relative to Drug Entry",size(large)) ytitle("Fraction on Generic Zocor",size(large))  ///
	xlabel(,labsize(large)) ylabel(,labsize(large)) ///
legend(label(1 "Treated") label(2 "Control Z") label(3 "Control L") label(4 "Control C") label(5 "Control V"))

graph export "event_study_results_generic_zocor.pdf", replace
graph save "event_study_results_generic_zocor", replace


restore

